<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
            <el-form-item label="昵称/姓名" prop="paymentName">
                <el-input v-model="queryParams.paymentName" placeholder="请输入昵称/姓名"
                          clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入手机号"
                          clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="结算类型" prop="paymentType">
                <el-select
                    v-model="queryParams.paymentType"
                    placeholder="请输入结算类型"
                    clearable
                    size="small"
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in income_type"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="账单周期">
                <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                                type="daterange" range-separator="-" :default-time="['00:00:00', '23:59:59']" start-placeholder="开始日期" end-placeholder="结束日期"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请输入状态"
                    clearable
                    size="small"
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in payment_status"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"-->
<!--                           v-hasPermi="['bs:payment:saveOrUpdate']"-->
<!--                >新增</el-button>-->
<!--            </el-col>-->
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleUpdateBatch"
                        v-hasPermi="['bs:payment:delete']"
                >批量打款</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                        v-hasPermi="['bs:payment:export']"
                >导出</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" :selectable="selectable"/>
            <el-table-column label="ID" align="center" prop="id" :show-overflow-tooltip="true" />
            <el-table-column label="账单周期" align="center" prop="checkTimes" :show-overflow-tooltip="true" width="240px"/>
            <el-table-column label="结算类型" align="center" prop="incomeType" :show-overflow-tooltip="true" :formatter="incomeTypeFormat" v-if="this.$store.getters.loginUser.roles[0].roleId !== 2"/>
            <el-table-column label="昵称/姓名" align="center" prop="paymentName" :show-overflow-tooltip="true" />
            <el-table-column label="手机号" align="center" prop="phone" :show-overflow-tooltip="true"  v-if="this.$store.getters.loginUser.roles[0].roleId !== 2"/>
            <el-table-column label="结算金额" align="center" prop="checkNum" :show-overflow-tooltip="true" />
            <el-table-column label="银行卡号" align="center" prop="bankNo" :show-overflow-tooltip="true" />
            <el-table-column label="开户银行" align="center" prop="bankDesc" :show-overflow-tooltip="true" />
            <el-table-column label="出账时间" align="center" prop="createTime" width="180" />
            <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true" :formatter="statusFormat"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="this.$store.getters.loginUser.roles[0].roleId !== 2">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                            v-hasPermi="['bs:payment:saveOrUpdate']">{{scope.row.status === '1' ? '打款' : "-"}}</el-button>
<!--                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"-->
<!--                            v-hasPermi="['bs:payment:saveOrUpdate']">修改</el-button>-->
<!--                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
<!--                            v-hasPermi="['bs:payment:delete']">删除</el-button>-->
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.nowPage" :limit.sync="queryParams.pageSize" @pagination="getPage"/>
        <!-- 查看结算管理详情对话框 -->
        <el-dialog :title="title" :visible.sync="showOpen" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="showForm" :model="showForm" :rules="rules" label-width="120px" >
                <el-form-item label="账单周期" prop="checkTimes">
                    <el-input v-model="showForm.checkTimes" style="width: 450px;"  type="text"  disabled placeholder="请输入账单周期" />
                </el-form-item>
                <el-form-item label="结算类型" prop="paymentType">
                    <el-radio-group v-model="showForm.paymentType" disabled="true">
                        <el-radio
                            v-for="dict in income_type"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="昵称/姓名" prop="paymentName">
                    <el-input v-model="showForm.paymentName" style="width: 450px;"  type="text"  disabled placeholder="请输入昵称/姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="showForm.phone" style="width: 450px;"  type="text"  disabled placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="结算金额" prop="checkNum">
                    <el-input v-model="showForm.checkNum" style="width: 450px;"  type="text"  disabled placeholder="请输入结算金额" />
                </el-form-item>
                <el-form-item label="银行卡号" prop="bankNo">
                    <el-input v-model="showForm.bankNo" style="width: 450px;"  type="text"  disabled placeholder="请输入银行卡号" />
                </el-form-item>
                <el-form-item label="开户银行" prop="bankDesc">
                    <el-input v-model="showForm.bankDesc" style="width: 450px;"  type="text"  disabled placeholder="请输入开户银行" />
                </el-form-item>
                <el-form-item label="出账时间" prop="outTime">
                    <el-date-picker type="datetime" v-model="showForm.outTime" format="yyyy-MM-dd HH:mm:ss"
                                    value-format="yyyy-MM-dd HH:mm:ss" :style="{width: '100%'}" disabled placeholder="请选择出账时间" clearable>
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelShow">关闭</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改结算管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px"  v-dialog-drag :close-on-click-modal='false'>
            <el-form ref="form" :model="form" label-width="120px" :disabled="true">
                <el-form-item label="账单周期" prop="checkTimes">
                    <el-input v-model="form.checkTimes" style="width: 450px;"  type="text" placeholder="请输入账单周期" />
                </el-form-item>
                <el-form-item label="结算类型" prop="paymentType">
                    <el-radio-group v-model="form.paymentType">
                        <el-radio
                            v-for="dict in income_type"
                            :key="dict.dictValue"
                            :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="昵称/姓名" prop="paymentName">
                    <el-input v-model="form.paymentName" style="width: 450px;"  type="text" placeholder="请输入昵称/姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="form.phone" style="width: 450px;"  type="text" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="结算金额" prop="checkNum">
                    <el-input v-model="form.checkNum" style="width: 450px;"  type="text" placeholder="请输入结算金额" />
                </el-form-item>
                <el-form-item label="银行卡号" prop="bankNo">
                    <el-input v-model="form.bankNo" style="width: 450px;"  type="text" placeholder="请输入银行卡号" />
                </el-form-item>
                <el-form-item label="开户银行" prop="bankDesc">
                    <el-input v-model="form.bankDesc" style="width: 450px;"  type="text" placeholder="请输入开户银行" />
                </el-form-item>
                <el-form-item label="出账时间" prop="outTime">
                    <el-date-picker type="datetime" v-model="form.outTime" format="yyyy-MM-dd HH:mm:ss"
                                    value-format="yyyy-MM-dd HH:mm:ss" style="width: 450px;" placeholder="请选择出账时间" clearable>
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {
    getBsPaymentPage,
    getBsPaymentById,
    addOrUpdateBsPayment,
    delBsPayment,
    exportBsPayment,
    updateBatch
} from "@/api/project/bsPayment/bsPayment";
export default {
    name: "bsPayment",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            //
            payments: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 选中行状态列表
            statusList: [],
            // 总条数
            total: 0,
            // 参数表格数据
            configList: [],
            // 弹出层标题
            title: "",
            open: false,// 是否显示修改或增加的弹出层
            // 日期范围
            dateRange: [],
            // 结算类型数据字典
            income_type: [],
            // 收益状态数据字典
            payment_status: [],
            // 查询参数
            queryParams: {
                nowPage: 1,
                pageSize: 10,
                checkTimes: undefined,
                paymentName: undefined,
                paymentType: undefined,
                phone: undefined,
                status: undefined,
            },
            // 表单参数
            form: {},
            showOpen: false,//是否显示查看工作内容弹出层
            showForm: {},
            // 表单校验
            rules: {
                bankDesc: [
                    {required: true, message: "开户银行不能为空", trigger: "blur"}
                ],
                bankNo: [
                    {required: true, message: "银行卡号不能为空", trigger: "blur"}
                ],
                checkNum: [
                    {required: true, message: "结算金额不能为空", trigger: "blur"}
                ],
                checkTimes: [
                    {required: true, message: "账单周期不能为空", trigger: "blur"}
                ],
                createBy: [
                    {required: true, message: "创建人不能为空", trigger: "blur"}
                ],
                paymentName: [
                    {required: true, message: "昵称/姓名不能为空", trigger: "blur"}
                ],
                paymentType: [
                    {required: true, message: "收益类型不能为空", trigger: "blur"}
                ],
                phone: [
                    {required: true, message: "手机号不能为空", trigger: "blur"}
                ],
                status: [
                    {required: true, message: "1待结算/2已结算不能为空", trigger: "blur"}
                ],
                updateBy: [
                    {required: true, message: "更新人不能为空", trigger: "blur"}
                ],
            },
        };
    },
    created() {
        this.getPage();
        this.getDicts("income_type").then(response => {
            this.income_type = response.data;
        });
        this.getDicts("payment_status").then(response => {
            this.payment_status = response.data;
        });
    },
    methods: {
        /** 查询参数列表 */
        getPage(e) {
            this.loading = true;
            let paymentName = this.queryParams.paymentName || '';
            let phone = this.queryParams.phone || '';
            let paymentType = this.queryParams.paymentType || '';
            let status = this.queryParams.status || '';
            let time = this.dateRange || '';
            let params = '';
            if (e) {
                params = {
                    paymentName: paymentName,
                    paymentType: paymentType,
                    phone: phone,
                    status: status,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: e.page,
                    pageSize: e.limit
                }
            } else {
                params = {
                    paymentName: paymentName,
                    paymentType: paymentType,
                    phone: phone,
                    status: status,
                    startTime: time[0],
                    endTime: time[1],
                    nowPage: this.queryParams.nowPage,
                    pageSize: this.queryParams.pageSize
                }
            }
            getBsPaymentPage(params).then(response => {
                this.configList = response.data.records;
                this.total = response.data.total;
                this.loading = false;
            })
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                bankDesc: undefined,
                bankNo: undefined,
                checkNum: undefined,
                checkTimes: undefined,
                createBy: undefined,
                createTime: undefined,
                paymentName: undefined,
                paymentType: undefined,
                outTime: undefined,
                phone: undefined,
                status: undefined,
                updateBy: undefined,
                updateTime: undefined,
            };
            this.showForm = {
                bankDesc: undefined,
                bankNo: undefined,
                checkNum: undefined,
                checkTimes: undefined,
                createBy: undefined,
                createTime: undefined,
                paymentName: undefined,
                paymentType: undefined,
                outTime: undefined,
                phone: undefined,
                status: undefined,
                updateBy: undefined,
                updateTime: undefined,
            };
            this.resetForm("form");
            this.resetForm("showForm");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            let paymentName = this.queryParams.paymentName || '';
            let phone = this.queryParams.phone || '';
            let paymentType = this.queryParams.paymentType || '';
            let status = this.queryParams.status || '';
            let time = this.dateRange || '';
            let params = {
                paymentName: paymentName,
                paymentType: paymentType,
                phone: phone,
                status: status,
                startTime: time[0],
                endTime: time[1],
                nowPage: 1,
                pageSize: this.queryParams.pageSize
            };
            getBsPaymentPage(params).then(res => {
                if (res.code == 200) {
                    this.configList = res.data.records;
                    this.total = res.data.total;
                    this.loading = false;
                }
            })
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.queryParams.bankDesc = '';
            this.queryParams.bankNo = '';
            this.queryParams.checkNum = '';
            this.queryParams.checkTimes = '';
            this.queryParams.createBy = '';
            this.queryParams.paymentName = '';
            this.queryParams.paymentType = '';
            this.queryParams.phone = '';
            this.queryParams.status = '';
            this.queryParams.updateBy = '';
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加结算管理信息";
        },
        //多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.payments = selection;
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 查看按钮操作 */
        handleShow(row) {
            this.reset();
            const id = row.id || '';
            getBsPaymentById({id}).then(response => {
                this.showForm = response.data;
                this.showOpen = true;
                this.title = "打款";
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || '';
            getBsPaymentById({id}).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "打款";
            });
        },
        /** 批量打款 */
        handleUpdateBatch() {
            this.payments.forEach(item =>item.status = '2');
            updateBatch(this.payments).then(res => {
                if (res.code == 200) {
                    this.msgSuccess("打款成功");
                    this.open = false;
                    this.getPage();
                } else {
                    this.msgError(res.msg);
                }
            })
        },
        cancelShow: function () {
            this.showOpen = false;
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id !== undefined) {
                        this.form.status = "2";
                        addOrUpdateBsPayment(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    } else {
                        addOrUpdateBsPayment(this.form).then(res => {
                            if (res.code == 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getPage();
                            } else {
                                this.msgError(res.msg);
                            }
                        })
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认结算选中的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delBsPayment(ids);
            }).then(() => {
                this.getPage();
                this.msgSuccess("删除成功");
            }).catch(function () {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有结算管理数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportBsPayment(queryParams);
            }).then(res => {
                this.download(res.msg);
            }).catch(function () {
            });
        },
        selectable(row) {
            return row.status === '1';
        },
        /**
         * 字典状态转换
         */
        incomeTypeFormat(row, column) {
            return this.selectDictLabel(this.income_type, row.paymentType);
        },
        /**
         * 字典状态转换
         */
        statusFormat(row, column) {
            return this.selectDictLabel(this.payment_status, row.status);
        },
    }
};
</script>
<style>

</style>
